<template>
  <div>
    <van-nav-bar title="收获地址" left-arrow @click-left="onClickLeft" />
    <div @click="aaa">
      <van-address-list
        v-model="chosenAddressId"
        :list="list"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit" />
    </div>
    <!-- @add="onAdd('AddressEdit',id)" -->
  </div>
</template>
<script>
export default {
  data() {
    return {
      chosenAddressId: 0,
      UserId: this.$route.query.id,
      list: [],
      product: {},
      addressA: {},
    };
  },
  created() {
    const dataParam = this.$route.query.data;
    if (dataParam) {
      const product = JSON.parse(dataParam);
      this.product = product;
    }
    this.loadData();
    //生命周期  创建后要做的事情  vue被创建时候执行的
  },
  methods: {
    loadData() {
      //不是直接使用axios 而是使用$http
      //baseURL="http://localhost:8080"
      var _this = this;
      this.$http
        .get("/address/queryAll")
        .then(function (response) {
          console.log(response);
          if (response.data.code == 200) {
            //this不可以直接使用 代表的是axios对象
            let listA = response.data.data;
            // alert(JSON.stringify(listA))
            for (var i = 0; i < listA.length; i++) {
              let b = false;
              if (listA[i].isDefault == "1") {
                b = true;
                _this.chosenAddressId = listA[i].addressId;
              }
              let a = {
                id: listA[i].addressId,
                name: listA[i].addressName,
                tel: listA[i].phone,
                address: listA[i].detail,
                isDefault: b,
                userId: listA[i].userId,
              };

              _this.list.push(a);
            }
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    onClickLeft() {
      this.$router.push({
        name: "singleOrder",
        query: {
          data: JSON.stringify(this.product),
        },
      });
    },
    onAdd() {
      //  this.$router.push({ name: uri, query: { userId: id } });
      this.$router.push({
        name: "AddressEdit",
        query: {
          UserId: this.list[0].userId,
          data: JSON.stringify(this.product),
        },
      });
    },
    onEdit(item, index) {
      let id = this.list[index].id;
      this.$router.push({
        name: "AddressOrderEdit",
        query: {
          id: id,
          UserId: this.list[0].userId,
          data: JSON.stringify(this.product),
        },
      });
    },
    aaa() {
      this.addressA.isDefault == true ? "1" : "0";
      this.addressA = this.list[this.chosenAddressId - 1];
      this.$router.push({
        name: "singleOrder",
        query: {
          address: JSON.stringify(this.addressA),
          data: JSON.stringify(this.product),
          UserId: this.list[0].userId,
        },
      });
    },
  },
  components: {
    // 组件注册的地方 相当于pom文件
  },
};
</script>
<style></style>
